<?php
class Work_facility extends CI_Controller 
{
	private $limit = 10;
	function __construct()
	{
		parent::__construct();
		if((!check_auth()) ? redirect("/login/auth") : site_url("/home"));
		$this->load->model('work_facility_model');
	}

	/*********************************************************
	**	MODULE ID Card and Telephone			**
	**	AUTHOR 		: ERY   			**
	**	DATE CREATED	: 19/05/2014			**
	*********************************************************/
	function id_card_and_telephone()
	{

		if(count($_POST) > 0)
		{
			$this->form_validation->set_rule();

		}
		else
		{
			$data['dtNow'] = date("d/m/Y",now());
			$data['workPer'] = $this->work_facility_model->getWorkPermission();
			$data['floor_active'] = $this->work_facility_model->arr_floor();
			$data['getReference'] = $this->work_facility_model->getReference();
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	function insert_cp() {
		$pCtrlUserType 		= trim($this->input->post('UserType')); 	//value= Telkomsel/Vendor
		$pCtrlUserName 		= trim($this->input->post('r_nama'));
		$nik 				= trim($this->input->post('r_id'));
		$pAtasanNama		= trim($this->input->post('r_mgr'));
		$pAtasanNik 		= trim($this->input->post('r_mgrid'));
		$pCtrlVendor		= trim($this->input->post('r_dept'));
		$pCardFisik 		= trim($this->input->post('pCardFisik')); 	//value= 0/10
		$pPhoneFisik 		= trim($this->input->post('pPhoneFisik')); 	//value= Analog/Digital
		$pCtrlSik 			= trim($this->input->post('pCtrlSik')); 	//Ref. Doc.
		$vDate1 			= trim($this->input->post('G_Date1'));
		$vDate2 			= trim($this->input->post('G_Date2'));
		
		$pFile = $_POST['file'];
            $tmpname  = $_FILES['file']['tmp_name'];
            $filename = $_FILES['file']['name'];
            $filesize = $_FILES['file']['size'];
            $filetype = $_FILES['file']['type'];
			
		$pCtrlDesc 			= str_replace("'", "''", $_POST['pCtrlDesc']);
		
		$arr['rFloor'] 		= trim($this->input->post('rFloor'));
		if (empty($arr['rFloor'])){
			$pCtrlCheck1 = "N"; 
		} else {
			$pCtrlCheck1 = "Y"; 
		}
		
		$arr['rLine'] 		= trim($this->input->post('rLine'));
		if (empty($arr['rLine'])){
			$pCtrlCheck2 = "N"; 
		} else {
			$pCtrlCheck2 = "Y"; 
		}
		
		$arr['ceklocal'] 		= trim($this->input->post('ceklocal'));
		if (empty($arr['ceklocal'])){
			$pCtrlLokal = "N"; 
		} else {
			$pCtrlLokal = "Y"; 
		}
		
		$arr['ceksljj'] 	= trim($this->input->post('ceksljj'));
		if (empty($arr['ceksljj'])){
			$pCtrlSljj = "N"; 
		} else {
			$pCtrlSljj = "Y"; 
		}
		
		$arr['ceksli'] 		= trim($this->input->post('ceksli'));
		if (empty($arr['ceksli'])){
			$pCtrlSli = "N"; 
		} else {
			$pCtrlSli = "Y"; 
		}
		
		if ($_FILES["file"]["error"] > 0) {
			if ($_FILES["file"]["error"]<>4) echo "Error: " . $_FILES["file"]["error"] . "<br />";
			$fname = '';
			$filename = '';
			exit;
		
		} else {
			
			$qryZ = "select max(ctrl_filename) xx from fm_access_control where lower(ctrl_filename) like 'foto_%' ";
			$result = $this->db->query($qryZ);
			
			$Udata = $result->row_array;
			$Rdata = $result->num_rows();
			if ($Rdata > 0){$fn1 = $Udata['XX'];}else{$fn1 = '';}
						
			$fn = preg_match('/^foto_(\d{5})/', $fn1, $match) ? str_pad(++$match[1], 5, '0', STR_PAD_LEFT) : '00001';
			$fext = ($x=strrpos($_FILES["file"]["name"], '.'))!==false ? substr($_FILES["file"]["name"], $x) : '';
			$fext = strtolower($fext);
			$fname = "foto_$fn$fext";
			$fdest = "upload/$fname";
			unset($fn, $fext);

			$res_upload = move_uploaded_file($_FILES["file"]["tmp_name"], $fdest);
			if ($res_upload) {
				chmod($fdest, 0644);				
			} else {
				$fname = '';
				$filename = '';
			}						
			
			$filename = str_replace("'", "x", $filename);

			$qry1 = "select max(to_number(substr(ctrl_id,1,5),'99999')) ctrl_id_old, rtrim(to_char(sysdate,'RM')) romawi from fm_access_control ";
			$a = $this->db->query($qry1)->row_array();
			
			$cCtrlIdOld = $a["CTRL_ID_OLD"];	
			$cRomawi    = $a["ROMAWI"];	
			$cCtrlIdNew = $cCtrlIdOld +1;
			
			$qry2 = "select lpad($cCtrlIdNew,5,0)||'/ACCESS/$cRomawi/'||TO_CHAR(SYSDATE,'YYYY') ctrl_id from dual";
			$b = $this->db->query($qry2)->row_array();
			
			$cCtrlId = $b["CTRL_ID"];	
			
			$stmt = " 
				insert into fm_access_control ( 
					ctrl_id, ctrl_nip, ctrl_request_date, ctrl_description, 
					usrupd, dtmupd, CTRL_ATASAN_ID, CTRL_DATE_AWAL, CTRL_DATE_AKHIR, 
					CTRL_UNTUK_NIK, CTRL_SIK_NO, CTRL_TELP_FISIK, CTRL_CARD_FISIK, 
					ctrl_file, ctrl_filename ) ".
					
				" values (
					'$cCtrlId', $nik, trunc(sysdate), '$pCtrlDesc', 
					$nik, trunc(sysdate), $pAtasanNik, to_date('$vDate1','dd/mm/yyyy'), to_date('$vDate2','dd/mm/yyyy'), 
					$nik, '$pCtrlSik', '$pPhoneFisik', '$pCardFisik', 
					'$filename', '$fname' ) ";
					
			$this->db->query($stmt);

			$msg = "Order No $cCtrlId telah berhasil, untuk Proses selanjutnya menunggu Approval dari atasan anda $pAtasanNama.";	
			$msgatasan = "FM : Order No $cCtrlId membutuhkan approval anda.";
			
			if(!empty($pCtrlCheck1) && $pCtrlCheck1 == 'Y') {
				
				$qry1 = "select max(to_number(substr(CTRL_CARD_ID,1,5),'99999')) ctrl_card_id_old, rtrim(to_char(sysdate,'RM')) romawi from fm_control_card ";
				$a = $this->db->query($qry1)->row_array();
				
				$cCtrlCardIdOld = $a["CTRL_CARD_ID_OLD"];	
				$cRomawi    = $a["ROMAWI"];	
				$cCtrlCardIdNew = $cCtrlCardIdOld +1;
			
				$qry2 = "select lpad($cCtrlCardIdNew,5,0)||'/DOC/$cRomawi/'||TO_CHAR(SYSDATE,'YYYY') ctrl_card_id from dual";
				$b = $this->db->query($qry2)->row_array();
				
				$cCtrCardlId = $b["CTRL_CARD_ID"];	
				
				$stmt =	" 
					insert into fm_control_card ( 
						ctrl_card_id, ctrl_id, ctrl_card_user, 
						ctrl_card_user_type, ctrl_card_vendor) ".
					"values ( 
						'$cCtrCardlId', '$cCtrlId', '$pCtrlUserName',
						'$pCtrlUserType', '$pCtrlVendor' ) ";
				
				$this->db->query($stmt);

				$msg = "FM : Order No $cCtrlId telah berhasil, untuk Proses selanjutnya menunggu Approval dari atasan anda $pAtasanNama.";	
				$msgatasan = "FM : Order No $cCtrlId membutuhkan approval anda.";

				// Start Sms
						
				// Sms to User
				//$msisdn = dbora_getgamsisdn($pNikFor);
				//send_sms($msisdn, $msg);
				//End Sms User
				
				// Sms to Atasan
				//$msisdnatasan = dbora_getgamsisdn($pAtasanNik);
				//send_sms($msisdnatasan, $msgatasan);
				//End Sms Atasan
				
				/*
				$getFloor = "select count(*) CNT_ROW
							 from (
							 SELECT flo.mst_floor_id
							 from fm_mst_floor flo
							 where flo.mst_floor_status = 'Y'
							 group by flo.mst_floor_id ) ";
				
				$row = $this->db->query($getFloor)->row_array();
				$cnt = $row['CNT_ROW'];
				*/
				
				$jumlah = trim($this->input->post('TotFloorRow'));
				for($x=0;$x<$jumlah;$x++){
					if (empty( $_POST['cekFloor'.$x] )){
						//$lantai[]=$_POST['pFloor'.$x];
						//$nama[]='N';
						
					}else{
						$lantai[]=$_POST['pFloor'.$x];
						//$nama[]='Y';
						
						$inscardfloor = " 
							insert into CONTROL_CARD_FLOOR ( 
								card_floor_id, card_floor_name, crtl_card_id ) 
							values ( 
								seq_card_floor.nextval, '$lantai[$x]', '$cCtrCardlId' ) 
						";
						
						$this->db->query($inscardfloor);
					}							
				}
			}
			
			if(!empty($pCtrlCheck2) && $pCtrlCheck2 == 'Y') {
				
				$qry1 = "select max(to_number(substr(CTRL_TELP_ID,1,5),'99999')) ctrl_TELP_id_old, rtrim(to_char(sysdate,'RM')) romawi from fm_control_telp";
				$a = $this->db->query($qry1)->row_array();
				
				$cCtrlTelpIdOld = $a["CTRL_TELP_ID_OLD"];	
				$cRomawi    	= $a["ROMAWI"];	
				$cCtrlTelpIdNew = $cCtrlTelpIdOld +1;
			
				$qry2 = "select lpad($cCtrlTelpIdNew,5,0)||'/DOC/$cRomawi/'||TO_CHAR(SYSDATE,'YYYY') ctrl_telp_id from dual";
				$b = $this->db->query($qry2)->row_array();
				
				$cCtrTelplId = $b["CTRL_TELP_ID"];	
				
				$stmt =	" 
					insert into fm_control_telp ( 
						ctrl_telp_id, ctrl_id, ctrl_telp_user, 
						ctrl_telp_user_type, ctrl_telp_vendor ) ".
					" values ( 
						'$cCtrTelplId', '$cCtrlId', '$pCtrlUserName', 
						'$pCtrlUserType', '$pCtrlVendor' ) ";
						
				$this->db->query($stmt);

				if(!empty($pCtrlLokal) && $pCtrlLokal == "Y") {
					$inscardfloor1 = "insert into CONTROL_TELP_EXT(telp_ext_id, TELP_EXT_EXT_TYPE, crtl_telp_id) values 
											(SQ_TELP_FLOOR.nextval, 'LOKAL', '$cCtrTelplId')";
					
					$this->db->query($inscardfloor1);
				}

				if(!empty($pCtrlSljj) && $pCtrlSljj == "Y") {
					$inscardfloor2 = "insert into CONTROL_TELP_EXT(telp_ext_id, TELP_EXT_EXT_TYPE, crtl_telp_id) values 
											(SQ_TELP_FLOOR.nextval, 'SLJJ', '$cCtrTelplId')";
					
					$this->db->query($inscardfloor2);
				}

				if(!empty($pCtrlSli) && $pCtrlSli == "Y") {
					$inscardfloor3 = "insert into CONTROL_TELP_EXT(telp_ext_id, TELP_EXT_EXT_TYPE, crtl_telp_id) values 
											(SQ_TELP_FLOOR.nextval, 'SLI', '$cCtrTelplId')";
					
					$this->db->query($inscardfloor3);
				}
			}
			
		}	
		
		$data['pesan'] = $msg;      
		$this->load->view('room_notification', $data);
		
	}
	
	/*********************************************************
	**	MODULE Parking Lot									**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/
	function parking_lot()
	{
		$Lcs_1 = '-';
		$Lcs_2 = '-';
		$Lcs_3 = '-';
		$Cert_1 = '-';
		$Cert_2 = '-';
		$Cert_3 = '-';
		$data['License1'] = '-';
		$data['License2'] = '-';
		$data['License3'] = '-';
		$data['Certificate1'] = '-';
		$data['Certificate2'] = '-';
		$data['Certificate3'] = '-';
		$data['Emp_Name'] = '';
		$data['Emp_Id'] = '';
		$data['Emp_Mgr'] = '';
		$data['Emp_Dept'] = '';
		$data['Emp_Job'] = '';
		$data['dtNow'] = date("d/m/Y",now());
		
		if(count($_POST) > 0)
		{	
			if($this->input->post('v_car') != "")
			{
				if($this->input->post('Emp_Name') == "")
				{
					$Error = array('danger' => 'Request Name must be fill');
					$this->session->set_userdata($Error);
					$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
				}
				else
				{
					if($Lcs_1 != "")
					{
						if($Cert_1 != "")
						{
							if($this->checkCar_License($Lcs_1) == FALSE)
							{
								$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
							}			
						}
						else
						{
							$Error = array('danger' => 'Vehicle Number Certificate No. 1 must be fill');
							$this->session->set_userdata($Error);
							$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
							//redirect($_SERVER['HTTP_REFERER']);
						}
						
						if($Lcs_2 != "")
						{
							if($Cert_2 != "")
							{
								if($this->checkCar_License($Lcs_2) == FALSE)
								{
									redirect($_SERVER['HTTP_REFERER']);
								}			
							}
							else
							{
								$Error = array('danger' => 'Vehicle Number Certificate No. 2 must be fill');
								$this->session->set_userdata($Error);
								redirect($_SERVER['HTTP_REFERER']);
							}	
						}
						
						if($Lcs_3 != "")
						{
							if($Cert_3 != "")
							{
								if($this->checkCar_License($Lcs_3) == FALSE)
								{
									redirect($_SERVER['HTTP_REFERER']);
								}		
							}
							else
							{
								$Error = array('danger' => 'Vehicle Number Certificate No. 3 must be fill');
								$this->session->set_userdata($Error);
								redirect($_SERVER['HTTP_REFERER']);
							}
						}
					}				
					else
					{
						$Error = array('danger' => 'Form No. 1 must be fill');
						$this->session->set_userdata($Error);
						$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
					}
				}
			}
			
			if($this->form_validation->run() == FALSE)
			{
				
			}
		}
		else
		{
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data); //Revisi by Surya Alam Gultom - Jumat, 22 Agustus 2014 03:50 PM
		}
	}
	
	function insert_parking()
	{
		$pName 				= trim($this->input->post('r_nama'));
		$pNik 				= trim($this->input->post('r_id'));
		$pAtasanNama		= trim($this->input->post('r_mgr'));
		$pAtasanNik 		= trim($this->input->post('r_mgrid'));
		$pVendor			= trim($this->input->post('r_dept'));
		
		$v_car 				= trim($this->input->post('v_car')); 
		$pParkirType1   	= "MOBIL";
		$pParkirType2   	= "MOBIL";
		$pParkirType3   	= "MOBIL";
		
		$NoLcs1 			= trim($this->input->post('NoLcs1')); 
		$NoCert1 			= trim($this->input->post('NoCert1')); 		
		
		$NoLcs2 			= trim($this->input->post('NoLcs2')); 
		$NoCert2 			= trim($this->input->post('NoCert2')); 		
		
		$NoLcs3 			= trim($this->input->post('NoLcs3')); 
		$NoCert3 			= trim($this->input->post('NoCert3')); 		
		
		$v_motor 			= trim($this->input->post('v_motor')); 	
		$pParkirType4   	= "MOTOR";
		
		$NoLcs4				= trim($this->input->post('NoLcs4')); 
		$NoCert4 			= trim($this->input->post('NoCert4')); 		
		
		$pStatusFor 		= trim($this->input->post('pStatusFor')); 	//value= PERMANENT/CONTRACT
		$vDate1 			= trim($this->input->post('G_Date2'));
		$vDate2 			= trim($this->input->post('G_Date3'));
	
		$pFile = $_POST['file'];
            $tmpname  = $_FILES['file']['tmp_name'];
            $filename = $_FILES['file']['name'];
            $filesize = $_FILES['file']['size'];
            $filetype = $_FILES['file']['type'];
			
		$pDesc 			= str_replace("'", "''", $_POST['Desc']);
		$pReqId			= ''; // Tidak Ada Parkir Referensi
		
		$arr['v_car'] 		= trim($this->input->post('v_car'));
		if (empty($arr['v_car'])){
			$pCtrlCheck1 = "N"; 
		} else {
			$pCtrlCheck1 = "Y"; 
		}
		
		$arr['v_motor'] 		= trim($this->input->post('v_motor'));
		if (empty($arr['v_motor'])){
			$pCtrlCheck2 = "N"; 
		} else {
			$pCtrlCheck2 = "Y"; 
		}
		
		$arr['NoRadio1'] 		= trim($this->input->post('NoRadio1'));
		if (empty($arr['NoRadio1'])){
			$NoRadio1 = "N"; 
		} else {
			$NoRadio1 = "Y"; 
			$pParkirType	= 'MOBIL';
			$pPlat			= $NoLcs1;
			$pStnk			= $NoCert1;
		}
		
		$arr['NoRadio2'] 		= trim($this->input->post('NoRadio2'));
		if (empty($arr['NoRadio2'])){
			$NoRadio2 = "N"; 
		} else {
			$NoRadio2 = "Y"; 
			$pParkirType	= 'MOBIL';
			$pPlat			= $NoLcs2;
			$pStnk			= $NoCert2;
		}
		
		$arr['NoRadio3'] 		= trim($this->input->post('NoRadio3'));
		if (empty($arr['NoRadio3'])){
			$NoRadio3 = "N"; 
		} else {
			$NoRadio3 = "Y"; 
			$pParkirType	= 'MOBIL';
			$pPlat			= $NoLcs3;
			$pStnk			= $NoCert3;
		}
		
		$arr['NoRadio4'] 		= trim($this->input->post('NoRadio4'));
		if (empty($arr['NoRadio4'])){
			$NoRadio4 = "N"; 
		} else {
			$NoRadio4 = "Y"; 
			$pParkirType	= 'MOTOR';
			$pPlat			= $NoLcs4;
			$pStnk			= $NoCert4;
		}
		
		if ($_FILES["file"]["error"] > 0) {
			if ($_FILES["file"]["error"]<>4) echo "Error: " . $_FILES["file"]["error"] . "<br />";
			$fname = '';
			$filename = '';
			exit;
			
		} else {
		
			$qryZ = "select max(parkird_filename) xx from fm_parkir_det where lower(parkird_filename) like 'park_%' ";
			$result = $this->db->query($qryZ);
			
			$Udata = $result->row_array;
			$Rdata = $result->num_rows();
			if ($Rdata > 0){$fn1 = $Udata['XX'];}else{$fn1 = '';}
			
			$fn = preg_match('/^park_(\d{5})/', $fn1, $match) ? str_pad(++$match[1], 5, '0', STR_PAD_LEFT) : '00001';
			$fext = ($x=strrpos($_FILES["file"]["name"], '.'))!==false ? substr($_FILES["file"]["name"], $x) : '';
			$fext = strtolower($fext);
			$fname = "park_$fn$fext";
			$fdest = "upload/$fname";
			unset($fn, $fext);

			$res_upload = move_uploaded_file($_FILES["file"]["tmp_name"], $fdest);
			if ($res_upload) {
				chmod($fdest, 0644);
			} else {
				$fname = '';
				$filename = '';
			}
						
		}
		
		$filename = str_replace("'", "x", $filename);

		$qry1 = "select max(to_number(substr(parkird_id,1,5),'99999')) parkird_id_old, rtrim(to_char(sysdate,'RM')) romawi from fm_parkir_det";
		$a = $this->db->query($qry1)->row_array();
			
		$cParkirdIdOld = $a["PARKIRD_ID_OLD"];	
		$cRomawi    = $a["ROMAWI"];	
		$cParkirdIdNew = $cParkirdIdOld +1;
		
		$qry2 = "select lpad($cParkirdIdNew,5,0)||'/PARK/$cRomawi/'||TO_CHAR(SYSDATE,'YYYY') parkird_id from dual";
		$b = $this->db->query($qry2)->row_array();
		
		$cParkirdId = $b["PARKIRD_ID"];	
		
		$stmt1	=	"insert into fm_parkir_det(parkird_id, parkird_nik, parkird_untuk_nik, parkird_use_by, 
											   parkird_type, parkird_plat, parkird_stnk,
											   parkird_type_1, parkird_plat_1, parkird_stnk_1, parkird_flag_1,
											   parkird_type_2, parkird_plat_2, parkird_stnk_2, parkird_flag_2,
											   parkird_type_3, parkird_plat_3, parkird_stnk_3, parkird_flag_3,
											   parkird_type_4, parkird_plat_4, parkird_stnk_4, parkird_flag_4,
											   parkird_description, parkird_atasan_nik, parkird_reference, parkird_date,
											   parkird_file, parkird_filetype, parkird_size, parkird_status, parkird_filename) ".
									" values ('$cParkirdId','$pNik','$pNik','$pStatusFor',
											  '$pParkirType','$pPlat','$pStnk',
											  '$pParkirType1','$NoLcs1','$NoCert1','$NoRadio1',
											  '$pParkirType2','$NoLcs2','$NoCert2','$NoRadio2',
											  '$pParkirType3','$NoLcs3','$NoCert3','$NoRadio3',
											  '$pParkirType4','$NoLcs4','$NoCert4','$NoRadio4',
											  '$pDesc','$pAtasanNik','$pReqId',trunc(sysdate),
											  '$filename','$filetype','$filesize','Y','$fname')";
		$this->db->query($stmt1);

		//$stmt2 = "update fm_req_det set req_det_app = 'Y' where req_id= '$pReqId' and req_det_id = '$pReqDetId'";
		//$this->db->query($stmt2);

		$msg = "FM : Order No $cParkirdId telah berhasil, untuk Proses selanjutnya menunggu Approval dari atasan anda $pAtasanNama.";
		$msgatasan = "FM : Order No $cParkirdId membutuhkan approval anda.";
		
		// Start Sms
				
		// Sms to User
		//$msisdn = dbora_getgamsisdn($pNikFor);
		//send_sms($msisdn, $msg);
		//End Sms User
		
		// Sms to Atasan
		//$msisdnatasan = dbora_getgamsisdn($pAtasanNik);
		//send_sms($msisdnatasan, $msgatasan);
		//End Sms Atasan
		
		$data['pesan'] = $msg;      
		$this->load->view('room_notification', $data);
	}
	
	function checkCar_License($pol)
	{
		$a_pol = substr_count($pol, ' ');
		if ($a_pol == 2)
		{
			$b_pol = explode(' ',$pol);
			if($this->alpha1($b_pol[0]) != TRUE)
			{
				$Error = array('danger' => 'Format is wrong ! First character must be alphabet (max 2 Characters)');
				$this->session->set_userdata($Error);
				return FALSE;
			}
			
			if($this->numeric($b_pol[1]) != TRUE)
			{
				$Error = array('danger' => 'Format is wrong ! Second character must be Number (max 4 Characters)');
				$this->session->set_userdata($Error);
				return FALSE;
			}
			
			if($this->alpha2($b_pol[2]) != TRUE)
			{
				$Error = array('danger' => 'Format is wrong ! Third character must be alphabet (max 3 Characters)');
				$this->session->set_userdata($Error);
				return FALSE;
			}
		}
		else
		{
			$Error = array('danger' => 'Format is wrong !');
			$this->session->set_userdata($Error);
			return FALSE;
		}
		return TRUE;
	}

	function validated_data($c)
	{
		if ($c == 1)
		{
			$this->form_validation->set_rules('Pol1','Vehicle License Number','callback_alpha_numeric_space|xss_clean');
			$this->form_validation->set_rules('License1','Vehicle Number Certificate No. 1','alpha_numeric_space|required|xss_clean');
			if(!$this->input->post('Pol2') == "")
			{
				$this->form_validation->set_rules('Pol2','Vehicle License Number','alpha_numeric_space|min_length[7]|max_length[9]|xss_clean');
				$this->form_validation->set_rules('License2','Vehicle Number Certificate No. 2','alpha_numeric_space|required|xss_clean');
			}
			
			if(!$this->input->post('Pol3') == "")
			{
				$this->form_validation->set_rules('Pol3','Vehicle License Number','alpha_numeric_space|min_length[7]|max_length[9]|xss_clean');
				$this->form_validation->set_rules('License3','Vehicle Number Certificate No. 3','alpha_numeric_space|required|xss_clean');
			}			
			if ($this->form_validation->run() !== false)
			{
				$Error = array('danger' => 'pass');
				$this->session->set_userdata($Error);
				redirect($_SERVER['HTTP_REFERER']);
			}
			else
			{
				$Error = array('danger' => validation_errors());
				$this->session->set_userdata($Error);
				redirect($_SERVER['HTTP_REFERER']);
			}
		}
	
	}
	
	function numeric($str)
	{
		return (! preg_match('/^[0-9]{1,4}$/',$str)) ? FALSE : TRUE;
	}
	
	function alpha1($str)
	{
		return ( ! preg_match("/^([a-z]){1,2}$/", $str)) ? FALSE : TRUE;
	}
	
	function alpha2($str)
	{
		return ( ! preg_match("/^([a-z]){1,3}$/", $str)) ? FALSE : TRUE;
	}
	
	
	/*********************************************************
	**	MODULE Overtime										**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/	
	function overtime()
	{
		if(count($_POST) > 0)
		{
			
		}
		else
		{
			$data['dtNow'] = date("d/m/Y",now());
			$data['ShowFloor1'] = $this->work_facility_model->getFloor();
			$data['ShowFloor2'] = $this->work_facility_model->getFloor();
			$data['ShowFloor3'] = $this->work_facility_model->getFloor();
			$data['ShowFloor4'] = $this->work_facility_model->getFloor();
			$data['ShowFloor5'] = $this->work_facility_model->getFloor();
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	function insert_ot() {
		$arr['pNik'] 		= trim($this->input->post('pNik'));
		$arr['pSdate'] 		= trim($this->input->post('pSdate'));    			
		$arr['pEdate'] 		= trim($this->input->post('pEdate'));    			
		$arr['pStime'] 		= trim($this->input->post('pStime'));   		    
		$arr['pEtime'] 		= trim($this->input->post('pEtime'));   		    
		$arr['pAc'] 		= trim($this->input->post('pAc'));    			   
		$arr['pLift'] 		= trim($this->input->post('pLift'));    			
		$arr['pLighting'] 	= trim($this->input->post('pLighting'));  		  
		$arr['pDescription']= trim($this->input->post('pDescription'));  		
		$arr['pAtasanNik'] 	= trim($this->input->post('pAtasanNik'));  		
		$arr['pAtasanNama'] = trim($this->input->post('pAtasanNama'));  		
		$arr['pNikFor'] 	= trim($this->input->post('pNikFor'));    		
		
		//UNTUK ISIAN TABEL BAWAH
		
		//Baris-1
		$arr['OT_Name1'] 		= trim($this->input->post('OT_Name1'));           
		$arr['OT_Floor1'] 		= trim($this->input->post('OT_Floor1'));          
		$arr['OT_Location1'] 	= trim($this->input->post('OT_Location1'));        
		$arr['OT_VendorId1'] 	= trim($this->input->post('OT_VendorId1'));        
		$arr['OT_VendorName1'] 	= trim($this->input->post('OT_VendorName1'));      
		$arr['OT_VendorPic1'] 	= trim($this->input->post('OT_VendorPic1'));		
		
		//Baris-2
		$arr['OT_Name2'] 		= trim($this->input->post('OT_Name2'));           
		$arr['OT_Floor2'] 		= trim($this->input->post('OT_Floor2'));          
		$arr['OT_Location2'] 	= trim($this->input->post('OT_Location2'));        
		$arr['OT_VendorId2'] 	= trim($this->input->post('OT_VendorId2'));        
		$arr['OT_VendorName2'] 	= trim($this->input->post('OT_VendorName2'));      
		$arr['OT_VendorPic2'] 	= trim($this->input->post('OT_VendorPic2')); 
		
		//Baris-3
		$arr['OT_Name3'] 		= trim($this->input->post('OT_Name3'));           
		$arr['OT_Floor3'] 		= trim($this->input->post('OT_Floor3'));          
		$arr['OT_Location3'] 	= trim($this->input->post('OT_Location3'));        
		$arr['OT_VendorId3'] 	= trim($this->input->post('OT_VendorId3'));        
		$arr['OT_VendorName3'] 	= trim($this->input->post('OT_VendorName3'));      
		$arr['OT_VendorPic3'] 	= trim($this->input->post('OT_VendorPic3'));
		
		//Baris-4
		$arr['OT_Name4'] 		= trim($this->input->post('OT_Name4'));           
		$arr['OT_Floor4'] 		= trim($this->input->post('OT_Floor4'));          
		$arr['OT_Location4'] 	= trim($this->input->post('OT_Location4'));        
		$arr['OT_VendorId4'] 	= trim($this->input->post('OT_VendorId4'));        
		$arr['OT_VendorName4'] 	= trim($this->input->post('OT_VendorName4'));      
		$arr['OT_VendorPic4'] 	= trim($this->input->post('OT_VendorPic4'));
		
		//Baris-5
		$arr['OT_Name5'] 		= trim($this->input->post('OT_Name5'));           
		$arr['OT_Floor5'] 		= trim($this->input->post('OT_Floor5'));          
		$arr['OT_Location5'] 	= trim($this->input->post('OT_Location5'));        
		$arr['OT_VendorId5'] 	= trim($this->input->post('OT_VendorId5'));        
		$arr['OT_VendorName5'] 	= trim($this->input->post('OT_VendorName5'));      
		$arr['OT_VendorPic5'] 	= trim($this->input->post('OT_VendorPic5'));
		
		$data['pesan'] = $this->work_facility_model->insert_ot($arr);      
		
		$this->load->view('room_notification', $data);
		$this->session->set_flashdata('Success', 'OT successfully inserted!');
    }
	
	/*********************************************************
	**	MODULE Repair And Maintenance						**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/	
	function repair_and_maintenance()
	{
		if(count($_POST) > 0)
		{
		
		}
		else
		{
			$data['dtNow'] = date("d/m/Y",now());
			$data['type_select'] = $this->work_facility_model->getTypeList();
			//$data['ShowFloor'] = $this->work_facility_model->getFloor();
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	function ShowRoomRequest() // Created by Surya Alam Gultom - Senin, 1 September 2014 10:11 AM
	{
		$type_name = $_POST['type_name'];
		$data['get_Room'] = $this->work_facility_model->getRoomForm($type_name);		
		$this->load->view('work_facility/ShowRoomRequest', $data);
	}
	
	function ShowFloorRequest() // Created by Surya Alam Gultom - Senin, 1 September 2014 11:13 AM
	{
		$room_name = $_POST['room_name'];
		$data['get_Floor'] = $this->work_facility_model->getFloorForm($room_name);		
		//var_dump ($data['get_Floor']); exit;
		$this->load->view('work_facility/ShowFloorRequest', $data);
	}
	
	function insert_rm() {
		$arr['r_id'] 		= trim($this->input->post('r_id'));
		$arr['r_nama'] 		= trim($this->input->post('r_nama'));
		$arr['r_mgr'] 		= trim($this->input->post('r_mgr'));
		$arr['r_mgrid'] 		= trim($this->input->post('r_mgrid'));
		$arr['r_dept'] 		= trim($this->input->post('r_dept'));
		$arr['r_job'] 		= trim($this->input->post('r_job'));		        
		$arr['type_name'] 	= trim($this->input->post('type_name'));
		$arr['room_name'] 	= trim($this->input->post('room_name'));
		$arr['floor'] 		= trim($this->input->post('floor'));
		$arr['serial'] 		= trim($this->input->post('serial'));
		$arr['G_Date1'] 	= trim($this->input->post('G_Date1'));
		$arr['G_Date2'] 	= trim($this->input->post('G_Date1'));
		$arr['Desc'] 		= trim($this->input->post('Desc'));
        
		$data['pesan'] = $this->work_facility_model->insert_rm($arr);        
        
		$this->load->view('room_notification', $data);
		$this->session->set_flashdata('Success', 'RM successfully inserted!');
    }

	/*********************************************************
	**	MODULE Request GA									**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/
	function request_ga()
	{
		if(count($_POST) > 0)
		{
		
		}
		else
		{	
			$data['dtNow'] = date("d/m/Y",now());
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	function insert_rga()
	{
		$pNama 				= trim($this->input->post('r_nama'));
		$pNik 				= trim($this->input->post('r_id'));
		$pNikFor			= $pNik;
		$pAtasanName		= trim($this->input->post('r_mgr'));
		$pAtasanNik 		= trim($this->input->post('r_mgrid'));
		$pVendor			= trim($this->input->post('r_dept'));
		
		$pAcpReqDescription = trim($this->input->post('pReqDescription'));
		$pReqInfoHrd 		= trim($this->input->post('pReqInfoHrd'));
		
		$qry1 = "select max(to_number(substr(req_id,1,5),'99999')) req_id_old, rtrim(to_char(sysdate,'RM')) romawi from fm_req_head ";
		$a = $this->db->query($qry1)->row_array();
		
		$cReqIdOld = $a["REQ_ID_OLD"];
		$cRomawi = $a["ROMAWI"];
		$cReqIdNew = $cReqIdOld + 1;

		$qry2 = "select lpad($cReqIdNew,5,0)||'/REQP/$cRomawi/'||TO_CHAR(SYSDATE,'YYYY') req_id from dual";
		$b = $this->db->query($qry2)->row_array();
		
		$cReqId = $b["REQ_ID"];
		
		$stmt1 = " 
			insert into fm_req_head ( 
				req_floor, req_rbf_id, req_id, req_nik, req_description, 
				req_info_hrd, req_date, req_atasan_nik, req_untuk_nik ) " .
			" values ( 
				'$pFloor','$pRoomFloor','$cReqId','$pNik','$pReqDescription', 
				'$pReqInfoHrd', sysdate, '$pAtasanNik','$pNikFor' ) 
			";

		$msg = "FM : Order No $cReqId telah berhasil, untuk Proses selanjutnya menunggu Approval dari atasan anda $pAtasanName.";
		$msgatasan = "FM : Order No $cReqId membutuhkan approval anda.";

		$msisdn = dbora_getgamsisdn($pNikFor);
		send_sms($msisdn, $msg);
		
		$msisdnatasan = dbora_getgamsisdn($pAtasanNik);
		send_sms($msisdnatasan, $msgatasan);
		
		$data['pesan'] = $msg;        
        $this->load->view('room_notification', $data);
	}

	/*********************************************************
	**	MODULE Work Permission								**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/
	function work_permission()
	{
		if(count($_POST) > 0)
		{
		
		}
		else
		{	
			$data['dtNow'] = date("d/m/Y",now());
			$data['ShowFloor1'] = $this->work_facility_model->getFloor();
			$data['ShowFloor2'] = $this->work_facility_model->getFloor();
			$data['ShowFloor3'] = $this->work_facility_model->getFloor();
			$data['ShowFloor4'] = $this->work_facility_model->getFloor();
			$data['ShowFloor5'] = $this->work_facility_model->getFloor();
			$data['ShowVendor'] = $this->work_facility_model->getVendor();
			
			//ambil session
			$CI =& get_instance();
			$data['sesName']=$CI->session->userdata('login')['name'];
			$data['sesJob']=$CI->session->userdata('login')['job'];
			$data['sesNik']=$CI->session->userdata('login')['nik'];
			$data['sesOrg']=$CI->session->userdata('login')['org'];
			
			$data['ShowMgr'] = $this->work_facility_model->getManager();
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	function ShowVendorDetail() // Created by Surya Alam Gultom - Selasa, 30 September 2014 12:15 PM
	{
		$pVendorId = $_POST['pVendorId'];
		$data['get_Vendor'] = $this->work_facility_model->getVendorForm($pVendorId);
		//var_dump ($data['get_Vendor']); exit;
		$this->load->view('work_facility/ShowVendorDetail', $data);
	}
	
	/*********************************************************
	**	MODULE GLOBALS	Get and Search NIK					**
	**	AUTHOR 			: ERY								**
	**	CREATED	DATE	: 19/05/2014						**
	*********************************************************/
	/*=======================================================
	 Function 		: Get All Data Employee
	 AUTHOR			: ERY
	 CREATED DATE	: 19/05/2014
	=======================================================*/
	/*=======================================================
	 MODIFIED DATE	: 22/05/2014
	 MODIFIED BY	: ERY
	 =======================================================*/	
	function globals_nik_Search()
	{
		$Emp_Name[] = SearchDetailEmp($this->security->xss_clean($this->input->post('SearchName')));
		foreach($Emp_Name AS $Emp_Name =>$keyVal)
		{
			$data['Show_Name'] = $keyVal['arr_Name'];
		}
		echo json_encode($data);
	}
	
	function globals_ven_Search()
	{
		$Ven_Name[] = SearchDetailVen($this->security->xss_clean($this->input->post('SearchName'),$this->input->post('nilai')));
        //$Ven_Name[] = SearchDetailVen($this->security->xss_clean($this->input->post('SearchName')));
		foreach($Ven_Name AS $Ven_Name =>$keyVal)
		{
			$data['Show_Name'] = $keyVal['arr_Name'];
		}
        //echo $this->input->post('SearchName'); exit();
		echo json_encode($data);
	}
	
	function globals_ven_detail()
	{
		$data['DetailVen'] = putDetailVen($this->security->xss_clean($this->input->post('id')));
		echo json_encode($data);
	}
	
	/*=======================================================
	 Function 		: Get employee data by search ID
	 				  from function_helper
	 AUTHOR			: ERY
	 CREATED DATE	: 19/05/2014
	=======================================================*/
	/*=======================================================
	 MODIFIED DATE	: 22/05/2014
	 MODIFIED BY	: ERY
	 =======================================================*/
	function globals_nik_detail()
	{
		$data['DetailEmp'] = putDetailEmp($this->security->xss_clean($this->input->post('id')));
		echo json_encode($data);
	}

	/*=======================================================
	 Function 		: Load static header and footer view page 
	 				  and dinamic page view page from each 
					  function 
	 AUTHOR			: ERY
	 CREATED DATE	: 19/05/2014
	=======================================================*/	
	function getView($P_Menu,$S_Menu, $data)
	{
		$this->load->view('header');
		if ($data)
		{
			//var_dump($data);
			$this->load->view($P_Menu.'/'.$S_Menu,$data);
		}
		else
		{
			var_dump($data);
			$this->load->view($P_Menu.'/'.$S_Menu);
		}
		$this->load->view('footer');
	}
}
?>